/**
 * 
 */
package com.acc.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import com.acc.dao.OrderDao;
import com.acc.dao.impl.OrderDaoImpl;
import com.acc.entity.Order;
import com.acc.entity.Standby;
import com.acc.service.OrderService;

/**
 * @author 姜伟康
 *2017-9-12
 */
public class OrderServiceImpl implements OrderService {
	
	OrderDao orderDao =new OrderDaoImpl();

	boolean result = false ;
	
	//显示所有订单
	@Override
	public List<Order> showAllOrder(Object...objects){
		// TODO Auto-generated method stub
		
	    String sql = "select * from (select rownum rn,ord.* from TI_ORDER ord  where username=? and rownum<=?) o where  o.rn>?";

		//通过姓名查找其所有车票
		List<Order> orders = orderDao.showAllOrder(sql,objects);
		
		return orders;
		
	}

	
	//查询订单总数
	@Override
	public int selectOrderNum(String username){
		// TODO Auto-generated method stub
		
		String sql = "select * from TI_ORDER where username=? ";
		
		int num = orderDao.showAllOrder(sql,username).size(); 
				
		return num;
	}



	// 新增订单
	@Override
	public boolean addOrder(String orderId ,String username,Integer standbyid) {
		// TODO Auto-generated method stub
		
		//根据车次查询车票
		 Standby standby = orderDao.selectStandbyById(standbyid);
		//新增订单
		int number = orderDao.addOrder( orderId ,username, standby);
     	//获取车次
		Integer trainNumber = standby.getId();
    	//减少票数
		orderDao.updateStandyAmount(-1,trainNumber);
		
		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}
	
	//支付订单
	@Override
	public boolean payOrder(String orderId)  {
		// TODO Auto-generated method stub
		
		//支付订单
		int number =orderDao.updateOrderStatus(orderId, "已支付");
		
		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}

	// 取消订单
	@Override
	public boolean deleteOrder(String orderId){
		// TODO Auto-generated method stub
    
		//通过订单号查询订单
		Order order = orderDao.selectOrder(orderId);
 		//获取车次
		Integer trainNumber = order.getTrainNumber();
 		//增加票数
		orderDao.updateStandyAmount(1,trainNumber);
		//取消订单
		int number = orderDao.deleteOrder(orderId);

		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}

	//改签
	@Override
	public boolean updateOrder(String orderId, Standby standby) {
		// TODO Auto-generated method stub
		
		//更新订单信息
		orderDao.updateOrder(orderId, standby);
		//通过订单号查询订单
		Order order = orderDao.selectOrder(orderId);
		//获取原车次
		Integer trainNumber1 = order.getTrainNumber();
		//获取改签车次
		Integer trainNumber2 = standby.getId();
		//增加原车次车票数,传参trainNumber1车次!
		orderDao.updateStandyAmount(1,trainNumber1);
		//减少改签车次车票数
		int number = orderDao.updateStandyAmount(-1,trainNumber2);
		
		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}

	//退票
	@Override
	public boolean returnOrder(String orderId) throws IOException, SQLException {
		// TODO Auto-generated method stub
		
		//退票
		int number = orderDao.updateOrderStatus(orderId, "已退票");
		//通过订单号查询订单
		Order order = orderDao.selectOrder(orderId);
		//获取车次
		Integer trainNumber = order.getTrainNumber();
		//增加票数
		orderDao.updateStandyAmount(1,trainNumber);
		
		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}

	//取票
	@Override
	public boolean takingOrder(String orderId) throws IOException, SQLException {
		// TODO Auto-generated method stub
		
		//取票
		int number = orderDao.updateOrderStatus(orderId, "已取票");
		//通过订单号查询订单
		Order order = orderDao.selectOrder(orderId);
		//获取车次
		Integer trainNumber = order.getTrainNumber();
	
		if (number!=0) {
	    	result = true ;
	}
	
		return result ;
	}


}
